package com.controller;

import com.entity.BGoods;
import com.entity.BType;
import com.entity.table.BGoodsTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.service.impl.BGoodsServiceImpl;
import com.service.impl.BTypeServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Resource
    BGoodsServiceImpl bGoodsService;
    @Resource
    BTypeServiceImpl bTypeService;

    @GetMapping(value = "")
    public String index(Model model){
        try {
            Map<String, List<BGoods>> goodList = new LinkedHashMap<>();
            //查询商品分类
            QueryWrapper wrapper = QueryWrapper.create().limit(6);
            List<BType> typeList = bTypeService.getMapper().selectListByQuery(wrapper);
            for (BType bType : typeList) {
                int typeId = bType.getId();
                String typeName = bType.getTypeName();
                //查询商品列表
                wrapper = QueryWrapper.create().where(BGoodsTableDef.B_GOODS.TYPE_ID.eq(typeId)).limit(4);
                List<BGoods> goods = bGoodsService.list(wrapper);
                goodList.put(typeName, goods);
            }
            model.addAttribute("goodType",typeList);
            model.addAttribute("goodList",goodList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "client/index";
    }
    //添加获取商品列表代码
    @GetMapping(value = "list")
    public String goodList(ModelMap model,
                           @RequestParam(value = "p", defaultValue = "1") int p,
                           @RequestParam(value = "pageSize", defaultValue = "10") int pageSize){
        try {
            //1.查询商品分类列表
            List<BType> typeList = bTypeService.list();
            //2.设置商品列表分页
            Page<BGoods> page = new Page<>();
            //设置每页显示记录数
            page.setPageSize(pageSize);
            //设置页码
            page.setPageNumber(p);
            //3.查询商品列表
            Page<BGoods> pageInfo = bGoodsService.page(page);
            //4.返回商品数据给前端
            model.addAttribute("page",pageInfo);
            //5.返回商品类型数据给前端
            model.addAttribute("goodType",typeList);
            return "client/list";
        } catch (Exception e) {
            e.printStackTrace();
            return "client/login";
        }
    }
    //添加修改商品页面代码
    @GetMapping(value = "goodDetail")
    public String goodDetail(int id, Model model) {
        try {
            //1.查询商品分类列表
            List<BType> typeList = bTypeService.list();
            //2.根据id查询商品
            BGoods bGood = bGoodsService.getById(id);
            //3.返回商品类型数据给前端
            model.addAttribute("typeList", typeList);
            //4.返回商品数据给前端
            model.addAttribute("bGood", bGood);
            return "client/detail";
        } catch (Exception e) {
            e.printStackTrace();
            return "client/login";
        }
    }
}
